package com.cndcare.msc.gui;

import com.cndcare.msc.util.IPUtils;
import com.cndcare.msc.util.LocalMac;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.RoundRectangle2D;
import java.math.BigDecimal;

/**
 * 右下角弹出式提示框
 * 1.自动上升
 * 2.停留一段时间，本例子中5秒
 * 3.自动下降直至消失
 * 4.线程控制窗口的出现和消失，同时添加鼠标事件控制，可以提前使提示框消失
 * 5.鼠标事件结合自己的需求实现，此处只是实现一个点击事件
 */
public class RightCornerPopMessage extends JWindow implements Runnable, MouseListener {

    private static final long serialVersionUID = -3564453685861233338L;
    private Integer screenWidth;  // 屏幕宽度
    private Integer screenHeight; // 屏幕高度
    private Integer windowWidth = 220; // 设置提示窗口宽度
    private Integer windowHeight = 85; // 设置提示窗口高度
    private Integer bottmToolKitHeight; // 底部任务栏高度，如果没有任务栏则为零
    private Integer x; // 窗口起始X坐标
    private Integer y; // 窗口起始Y坐标
    private String title = "主机监控信息";
    private JPanel mainPanel; // 主面板
    private JLabel titleLabel; // 标题栏标签
    private JPanel titlePanel; // 标题栏面板
    private JLabel messageLabel; // 内容标签
    private JPanel messagePanel; // 内容面板

    public static RightCornerPopMessage getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private RightCornerPopMessage singleton;

        // JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new RightCornerPopMessage();
        }

        public RightCornerPopMessage getInstance() {
            return singleton;
        }
    }

    public RightCornerPopMessage() {
        this.init();
        Thread thread = new Thread(this);
        thread.start();
    }

    private void init() {
        bottmToolKitHeight = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        screenWidth = dimension.width;
        screenHeight = dimension.height;

        x = screenWidth;
        y = 30;
        this.setLocation(x - 1, y);
        mainPanel = new JPanel(new BorderLayout());

        titleLabel = new JLabel(title);
        titleLabel.setForeground(Color.WHITE);
        titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        titlePanel.setBackground(new Color(33,186, 197));
        titlePanel.add(titleLabel);

        String txt = "<html><body>";
        txt += "MAC地址：" + LocalMac.getLocalMac() + "<br>";
        txt += "　　　IP：" + IPUtils.getRealIp() + "<br>";
        txt += "";
        txt += "</body></html>";
        messageLabel = new JLabel(txt);
        messagePanel = new JPanel();
        messagePanel.add(messageLabel);
        messagePanel.setBackground(Color.white);

        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(messagePanel, BorderLayout.CENTER);

        MouseEventListener mouseListener = new MouseEventListener(this);
        this.addMouseListener(mouseListener);
        this.addMouseMotionListener(mouseListener);
        this.setSize(windowWidth, windowHeight);
        this.setAlwaysOnTop(true);
        this.getContentPane().add(mainPanel);
        this.addMouseListener(this);
        this.setVisible(true);
        this.setOpacity(0.9f);
    }

    @Override
    public void run() {
        Integer delay = 30;
        Integer step = 1;
        while (x > screenWidth - windowWidth && x <= screenWidth) {
            try {
                step++;
                x = x - step;
                this.setLocation(x, y);
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        Point point = this.getLocation();
        int x = new BigDecimal(point.getX()).intValue();
        int y = new BigDecimal(point.getY()).intValue();
        if (x > screenWidth - windowWidth && x <= screenWidth - 10) {
            Integer delay = 10;
            Integer step = 10;
            while (x > screenWidth - windowWidth) {
                try {
                    x = x - step;
                    this.setLocation(x, y);
                    Thread.sleep(delay);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @SneakyThrows
    @Override
    public void mouseExited(MouseEvent e) {
        Point p = this.getLocation();
        int x = p.x;
        int y = p.y;
        if (x == screenWidth - windowWidth) {
            x = screenWidth - windowWidth;

            Integer delay = 10;
            Integer step = 5;
            Integer end = screenWidth - 10;
            while (x < end) {
                x = x + step;
                this.setLocation(x, y);
                Thread.sleep(delay);
            }
        }
    }

    class MouseEventListener implements MouseInputListener {

        Point origin;
        //鼠标拖拽想要移动的目标组件
        RightCornerPopMessage frame;

        public MouseEventListener(RightCornerPopMessage frame) {
            this.frame = frame;
            origin = new Point();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        /**
         * 记录鼠标按下时的点
         */
        @Override
        public void mousePressed(MouseEvent e) {
            origin.x = e.getX();
            origin.y = e.getY();
        }

        @SneakyThrows
        @Override
        public void mouseReleased(MouseEvent e) {
            Point p = this.frame.getLocation();
            int x = p.x;
            int y = p.y;
            if (x == screenWidth - windowWidth) {
                x = screenWidth - windowWidth;

                Integer delay = 10;
                Integer step = 10;
                Integer end = screenWidth - 10;
                while (x < end) {
                    x = x + step;
                    this.frame.setLocation(x, y);
                    Thread.sleep(delay);
                }
            }
        }

        /**
         * 鼠标移进标题栏时，设置鼠标图标为移动图标
         */
        @Override
        public void mouseEntered(MouseEvent e) {
            this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        }

        /**
         * 鼠标移出标题栏时，设置鼠标图标为默认指针
         */
        @Override
        public void mouseExited(MouseEvent e) {
            this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }

        /**
         * 鼠标在标题栏拖拽时，设置窗口的坐标位置
         * 窗口新的坐标位置 = 移动前坐标位置+（鼠标指针当前坐标-鼠标按下时指针的位置）
         */
        @SneakyThrows
        @Override
        public void mouseDragged(MouseEvent e) {
            Point p = this.frame.getLocation();
            int x = p.x + (e.getX() - origin.x);
            int y = p.y + (e.getY() - origin.y);
            if (x > screenWidth - windowWidth) {
                x = screenWidth - windowWidth;
            } else if (x < 0) {
                x = 0;
            }
            if (y > screenHeight - windowHeight) {
                y = screenHeight - windowHeight;
            } else if (y < 0) {
                y = 0;
            }
            this.frame.setLocation(x, y);
        }

        @Override
        public void mouseMoved(MouseEvent e) {

        }

    }

    public static void main(String[] args) {
        RightCornerPopMessage.getInstance();
    }
}